package inits

import (
	"Serve/conf"
	"Serve/global/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func Init() {
	conf.ReadAppConfig()
	MysqlConn()
}

func MysqlConn() {
	var err error
	model.DB, err = gorm.Open(mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", model.AppConf.Mysql.User, model.AppConf.Mysql.Password, model.AppConf.Mysql.Host, model.AppConf.Mysql.Port, model.AppConf.Mysql.Database)))
	if err != nil {
		panic(err)
	}
	if err = model.DB.AutoMigrate(&model.Video{}, &model.User{}, &model.ShowLog{}, &model.Class{}, &model.Audit{}); err != nil {
		panic(err)
	}
	fmt.Println("mysql init success")
}
